package com.baseframe.user.mapper;



import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.tool.result.R;
import com.baseframe.user.entity.BaseUser;
import com.baseframe.user.entity.request.BaseUserRequest;
import com.baseframe.user.entity.vo.BaseUserVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**

* @description 针对表【base_user(用户表)】的数据库操作Mapper
* @createDate 2023-05-20 19:25:46
* @Entity com.baseframe.system.entity.BaseUser
*/
public interface BaseUserMapper extends BaseMapper<BaseUser> {


    @Select("<script>" +
            "select bu.*,br.role_name as role_name from base_user as bu left join base_role as br on bu.role_id = br.id" +
            "<where>" +
            "bu.is_deleted = 0 " +
            "<if test=\" request.username!=null &amp;&amp; request.username !='' \">" +
            "and bu.username like concat('%',#{request.username},'%')" +
            "</if>" +
            "</where>" +
            "</script>")
    @Results(
            @Result(column = "role_name",javaType = String.class,property = "roleName")
    )
    Page<BaseUserVO> getUserPage(Page<BaseUserVO> page,@Param("request") BaseUserRequest request);

    @Select("select bu.*,br.role_name as role_name from base_role as br  left join base_user as bu on br.id = bu.role_id where bu.id = #{userId}")
    @Results(
            @Result(column = "role_name",javaType = String.class,property = "roleName")
    )
    BaseUserVO getUserInfo(@Param("userId") Long userId);

    @Select("<script>" +
            "select id, username,role_id, nickname, avatar, email, phone, gender, reg_time from base_user" +
            "<where>" +
            "is_deleted = 0 " +
            "<if test=\" searchValue!=null &amp;&amp; searchValue !='' \">" +
            "and username like concat('%',#{searchValue},'%') or nickname like concat('%',#{searchValue},'%')" +
            "</if>" +
            "</where>" +
            "order by create_time" +
            "</script>")
    List<BaseUserVO> getUserPage2Front(@Param("searchValue") String searchValue);


//    @Select("select * from base_user where username = #{username}")
//    @Results({
//                @Result(property = "roleId",column = "role_id"),
//                @Result(property = "permissions",
//                        column = "role_id",
//                        javaType = List.class,
//                        many = @Many(select = "findPermissionsByRoleId")
//                ),
//                @Result(property = "role",
//                        column = "role_id",
//                        javaType = BaseRole.class,
//                        one = @One(select = "findRoleByRoleId")
//                )
//            })
//    public BaseUser findAllByUsername(@Param("username") String username);
//
//
//    @Select("select * from base_user where username = #{phone}")
//    @Results({
//            @Result(property = "roleId",column = "role_id"),
//            @Result(property = "permissions",
//                    column = "role_id",
//                    javaType = List.class,
//                    many = @Many(select = "findPermissionsByRoleId")
//            ),
//            @Result(property = "role",
//                    column = "role_id",
//                    javaType = BaseRole.class,
//                    one = @One(select = "findRoleByRoleId")
//            )
//    })
//    public BaseUser findAllByPhone(@Param("phone") String phone);
//
//    @Select("select * from base_role where id = #{roleId}")
//    public BaseRole findRoleByRoleId(@Param("roleId")Long roleId);
//
//    @Select("select P.* from base_role as r left join base_permission as p on p.id in(select rs.permission_id from base_role_scope as rs where rs.role_id=r.id)  where r.id = #{roleId}")
//    public List<BasePermission> findPermissionsByRoleId(@Param("roleId")Long roleId);

}




